package com.ohimy.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.ohimy.util.DateFormat;

/**
 * 群组论坛主题封装类
 * 
 * @author Tony Tang
 * @version 1.0
 */
@Entity
@MappedSuperclass
@Table(name = "ohimy_forum")
public class Forum implements Serializable {

	private static final long serialVersionUID = -8358050920781113542L;

	private long id;

	private Group group; // 所属圈子ID

	private String title; // 贴子主题

	private String content; // 帖子内容

	private User publisher; // 发布人

	private Date created; // 发布时间

	private long viewTime; // 浏览次数

	private Date lastPublish; // 最后一次发表时间

	private String createdFormat; // 格式化时间，不映射

	private String lastFormat;// 格式化时间，不映射

	private List<ForumBody> forumBody;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY, optional = false)
	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	@Column(name = "_title")
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Lob
	@Column(name = "_content")
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY, optional = false)
	public User getPublisher() {
		return publisher;
	}

	public void setPublisher(User publisher) {
		this.publisher = publisher;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "_created", updatable = false)
	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	@Column(name = "_viewTime")
	public long getViewTime() {
		return viewTime;
	}

	public void setViewTime(long viewTime) {
		this.viewTime = viewTime;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "_lastPublish")
	public Date getLastPublish() {
		return lastPublish;
	}

	public void setLastPublish(Date lastPublish) {
		this.lastPublish = lastPublish;
	}

	@OneToMany(mappedBy = "forum", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
	public List<ForumBody> getForumBody() {
		return forumBody;
	}

	public void setForumBody(List<ForumBody> forumBody) {
		this.forumBody = forumBody;
	}

	@Transient
	public String getCreatedFormat() {
		createdFormat = DateFormat.format(created);
		return createdFormat;
	}

	public void setCreatedFormat(String createdFormat) {
		this.createdFormat = createdFormat;
	}

	@Transient
	public String getLastFormat() {
		lastFormat = DateFormat.format(lastPublish);
		return lastFormat;
	}

	public void setLastFormat(String lastFormat) {
		this.lastFormat = lastFormat;
	}

}
